package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import java.util.Date;

import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.User;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.UserRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.InitCreatorRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lilh
 * @date 2019-01-22 17:06
 */
@Service
public class InitCreatorRepositoryImpl implements InitCreatorRepository {

    @Autowired
    private UserRepository userRepository;

    @Override
    public <T> T init(T target) {
        CreatorInfo creatorInfo = new CreatorInfo();
        User current = userRepository.getCurrentUser();
        creatorInfo.setCreatorId(current.getId());
        creatorInfo.setCreatorName(current.getUserName());
        creatorInfo.setCreateTime(new Date());
        BeanUtils.copyProperties(creatorInfo, target);
        return target;
    }

    public static class CreatorInfo {

        private Integer creatorId;

        private String creatorName;

        private Date createTime;

        public Integer getCreatorId() {
            return creatorId;
        }

        public void setCreatorId(Integer creatorId) {
            this.creatorId = creatorId;
        }

        public String getCreatorName() {
            return creatorName;
        }

        public void setCreatorName(String creatorName) {
            this.creatorName = creatorName;
        }

        public Date getCreateTime() {
            return createTime;
        }

        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
    }
}
